package com.itheima.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mapper.GoodsSpecMapper;
import com.itheima.pojo.Spec;
import com.itheima.service.GoodsSpecService;
import com.itheima.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("GoodsSpecService")
public class GoodsSpecServiceImpl implements GoodsSpecService {

    @Autowired
    private GoodsSpecMapper goodsSpecMapper;

    @Override
    public Result findAllByPageGoodsSpec(int currentPage, int pageSize) {
        IPage<Spec> page = new Page<>(currentPage,pageSize);
        IPage<Spec> specIPage = goodsSpecMapper.selectPage(page, null);
        if (specIPage == null ) {
            return new Result(false, "查询失败");
        }
        return new Result<>(true,"查询成功",specIPage);
    }

    @Override
    public Result findAllGoodsSpec() {
        List<Spec> list = goodsSpecMapper.selectList(null);
        if (list == null) {
            return new Result(false, "查询失败");
        }
        return new Result<>(true, "查询成功", list);
    }

    @Override
    public Result addGoodsSpec(Spec spec) {
        int insert = goodsSpecMapper.insert(spec);
        if (insert != 1) {
            return new Result(false, "添加失败");
        }
        return new Result(true,"添加成功");
    }

    @Override
    public Result findByIdGoodsSpec(Long id) {
        Spec spec = goodsSpecMapper.selectById(id);
        if (spec == null) {
            return new Result(false, "查询失败");
        }
        return new Result(true, "查询成功", spec);
    }

    @Override
    public Result updateByIdGoodsSpec(long id ,Spec spec) {
        Spec selectById = goodsSpecMapper.selectById(id);
        if (spec == null) {
            return new Result(false, "修改失败，该商品信息不存在");
        }

        spec.setId(Integer.parseInt(String.valueOf(id)));

        int i = goodsSpecMapper.updateById(spec);
        if (i == 0) {
            return new Result(false,"修改失败") ;
        }
        return new Result(true,"修改成功") ;
    }

    @Override
    public Result deleteByIdGoodsSpec(long id) {
        int i = goodsSpecMapper.deleteById(id);
        if (i == 0) {
            return new Result(false, "删除失败");
        }
        return new Result(true,"删除成功");
    }


}
